/*Benny Vermonden c1025695*/

package views.searchViews;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.ListSelectionModel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JList;
import javax.swing.JButton;

import core.models.dataModels.searchModels.ZoekEigenaarModel;
import core.models.domainModel.EigenaarOverzicht;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JScrollPane;
import javax.swing.JRadioButton;

import controllers.searchControllers.ZoekEigenaarController;

public class ZoekEigenaarView extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JTextField txt_naam;
	private JTextField txt_voornaam;
	private JTextField txt_straat;
	private JTextField txt_huisnummer;
	private JTextField txt_postcode;
	private JTextField txt_gemeente;
	private JTextField txt_land;
	private ZoekEigenaarModel _model;
	private JList<EigenaarOverzicht> _gevondenEigenaarsJList;
	JRadioButton rdbtnPersoonEigenaar = new JRadioButton("Persoon Eigenaar");
	JRadioButton rdbtnMaatschappijEigenaar = new JRadioButton(
			"Maatschappij Eigenaar");
	JRadioButton rdbtnBeide = new JRadioButton("Beide");

	// private ZoekEigenaarModel listModel = new ZoekEigenaarModel();
	private DefaultListModel<EigenaarOverzicht> listModel = new DefaultListModel<EigenaarOverzicht>();

	public ZoekEigenaarView(ZoekEigenaarModel model) {
		_gevondenEigenaarsJList = new JList<EigenaarOverzicht>(listModel);
		_gevondenEigenaarsJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		_model = model;

		JLabel lblEigenaarZoeken = new JLabel("Eigenaar zoeken:");

		JLabel label = new JLabel("Naam:");

		txt_naam = new JTextField();
		txt_naam.setColumns(10);

		JLabel label_1 = new JLabel("Voornaam:");

		txt_voornaam = new JTextField();
		txt_voornaam.setColumns(10);

		JLabel label_2 = new JLabel("Straat:");

		txt_straat = new JTextField();
		txt_straat.setColumns(10);

		JLabel label_3 = new JLabel("Huisnummer:");

		txt_huisnummer = new JTextField();
		txt_huisnummer.setColumns(10);

		JLabel label_4 = new JLabel("Postcode:");

		txt_postcode = new JTextField();
		txt_postcode.setColumns(10);

		JLabel label_5 = new JLabel("Gemeente:");

		txt_gemeente = new JTextField();
		txt_gemeente.setColumns(10);

		JLabel label_6 = new JLabel("Land:");

		txt_land = new JTextField();
		txt_land.setColumns(10);

		JButton btnZoeken = new JButton("Zoeken");
		btnZoeken.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				listModel.clear();

				_model.set_gemeente(txt_gemeente.getText());
				_model.set_huisnummer(txt_huisnummer.getText());
				_model.set_land(txt_land.getText());
				_model.set_postcode(txt_postcode.getText());
				_model.set_straat(txt_straat.getText());
				_model.setNaam(txt_naam.getText());
				_model.setVoornaam(txt_voornaam.getText());

				ZoekEigenaarController.handleView(_model);
				for (EigenaarOverzicht item : _model.getGevondenEigenaars()) {
					listModel.addElement(item);
				}
			}
		});

		JButton btnWisZoekcriteria = new JButton("Wis zoekcriteria");
		btnWisZoekcriteria.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				txt_gemeente.setText("");
				txt_huisnummer.setText("");
				txt_land.setText("");
				txt_naam.setText("");
				txt_postcode.setText("");
				txt_straat.setText("");
				txt_voornaam.setText("");
			}
		});

		ButtonGroup bg = new ButtonGroup();
		bg.add(rdbtnPersoonEigenaar);
		rdbtnPersoonEigenaar.addActionListener(this);
		bg.add(rdbtnMaatschappijEigenaar);
		rdbtnMaatschappijEigenaar.addActionListener(this);
		bg.add(rdbtnBeide);
		rdbtnBeide.setSelected(true);
		rdbtnBeide.addActionListener(this);

		JScrollPane scrollPaneZoekresultaten = new JScrollPane(
				_gevondenEigenaarsJList);

		JLabel lblGevondenEigenaars = new JLabel("Gevonden eigenaars:");

		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(btnZoeken)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnWisZoekcriteria)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblGevondenEigenaars))
							.addComponent(scrollPaneZoekresultaten, GroupLayout.PREFERRED_SIZE, 402, GroupLayout.PREFERRED_SIZE)
							.addGroup(groupLayout.createSequentialGroup()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(label, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
									.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
									.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
									.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
									.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
									.addComponent(label_5, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
									.addComponent(label_6, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
								.addGap(30)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(txt_land, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
									.addComponent(txt_gemeente, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
									.addComponent(txt_postcode, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
									.addComponent(txt_huisnummer, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
									.addComponent(txt_straat, GroupLayout.PREFERRED_SIZE, 263, GroupLayout.PREFERRED_SIZE)
									.addComponent(txt_voornaam, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE)
									.addComponent(txt_naam, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE))))
						.addComponent(lblEigenaarZoeken))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(rdbtnPersoonEigenaar)
						.addComponent(rdbtnMaatschappijEigenaar)
						.addComponent(rdbtnBeide))
					.addContainerGap(106, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblEigenaarZoeken)
							.addGap(11)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(18)
									.addComponent(rdbtnMaatschappijEigenaar)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(rdbtnBeide))
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(21)
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(label_1)
										.addComponent(txt_voornaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addGap(7)
									.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(label_2)
										.addComponent(txt_straat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addGroup(groupLayout.createSequentialGroup()
											.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
												.addComponent(label_3)
												.addComponent(txt_huisnummer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
											.addPreferredGap(ComponentPlacement.RELATED)
											.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
												.addComponent(label_4)
												.addComponent(txt_postcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
											.addPreferredGap(ComponentPlacement.RELATED)
											.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
												.addComponent(label_5)
												.addComponent(txt_gemeente, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
											.addPreferredGap(ComponentPlacement.RELATED)
											.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
												.addComponent(label_6)
												.addComponent(txt_land, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
											.addPreferredGap(ComponentPlacement.RELATED)
											.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
												.addComponent(btnZoeken)
												.addComponent(btnWisZoekcriteria))
											.addGap(3))
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(lblGevondenEigenaars)
											.addPreferredGap(ComponentPlacement.RELATED)))
									.addComponent(scrollPaneZoekresultaten, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(31)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(label)
								.addComponent(txt_naam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(rdbtnPersoonEigenaar))))
					.addContainerGap(158, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == rdbtnMaatschappijEigenaar) {
			txt_voornaam.setText("");
			txt_voornaam.setEnabled(false);
		} else
			txt_voornaam.setEnabled(true);

	}
}
